package kindergarden.at.google;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;


/**
 *
 * @author paja0902
 *
 */
public class GUI extends JFrame implements ActionListener, TableModelListener
{
    private     JMenuBar menuBar;
    private     JMenuItem exit;
    private     JMenuItem writeOut;
    private     JMenuItem save;
    private     JMenuItem addChild;
    public      static JTable table;
    public      static JTextArea info;

    DatabaseConnection a;


    public GUI() throws ClassNotFoundException
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e)
        {
            System.err.println("Failed to load windows look and feel");
            e.printStackTrace();
        }

        try
        {
            a = new DatabaseConnection();
            table = new JTable(a.getResultSetTableModel("SELECT name, sin_num, attendence FROM child"));
            table.getSelectionModel().addListSelectionListener(new RowListener());
            table.getModel().addTableModelListener(this);
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            
        }
        catch (SQLException ex)
        {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }


        this.setLayout(new BorderLayout());

        JScrollPane sp = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        this.add(sp, BorderLayout.CENTER);

        this.add(menu(), BorderLayout.NORTH);
        this.add(ChildInfoDisplay(), BorderLayout.EAST);
        
        this.setTitle("Kinder aus kraus");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        //Display the window.
        this.pack();
        this.setVisible(true);
        this.setSize(500, 500);


    }


    public static void main(String[] args)
    {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    new GUI();
                } 
                catch (ClassNotFoundException ex)
                {
                    Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                } 

            }
        });
    }


    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == exit)
            System.exit(0);

        //Open up a form if a new child should be added
        if(e.getSource() == addChild)
            new form();
    }


    private JMenuBar menu()
    {
         //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        JMenu archive = new JMenu("Arkiv");
        JMenu help = new JMenu("Hjälp");

        exit = new JMenuItem("Avsluta");
        writeOut = new JMenuItem("Skriv ut...");
        save = new JMenuItem("Spara");
        addChild = new JMenuItem("Nytt barn");

        exit.addActionListener(this);
        addChild.addActionListener(this);

        archive.add(addChild);
        archive.add(save);
        archive.add(writeOut);
        archive.add(exit);

        menuBar.add(archive);
        menuBar.add(help);

        return menuBar;
    }

    public JScrollPane ChildInfoDisplay()
    {
        info = new JTextArea("");
        info.setBackground(Color.red);
        info.setEditable(true);
        JScrollPane scroll = new JScrollPane(info);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        return scroll;
    }

    @Override
    public void tableChanged(TableModelEvent e)
    {
        System.out.println("CHANGED");
        try
        {
            table = new JTable(a.getResultSetTableModel("SELECT name, sin_num, attendence FROM child"));
        }
        catch (SQLException ex)
        {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }





}